package top.lidee.module.clientApi.twice.applet.authorization;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.service.WeixinService;
import top.lidee.framework.annoation.AutAccess;
import top.lidee.framework.constant.NotesConstant;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.local.LoginUserLoc;
import top.lidee.repository.model.twice.vo.applet.AppWxLoginVo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/9/24 10:04
 */
@Api(tags="会员端-小程序登录相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/applet/user/sign")
public class MtUserSignController {

   /**
     * 微信相关接口
     * */
    private WeixinService weixinService;

    /**
     * 微信小程序登录
     */
    @AutAccess
    @ApiOperation(value = "微信小程序用户登录", notes = NotesConstant.TEST)
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult login(@RequestBody @Valid AppWxLoginVo vo) {
        /*
          方案一、
          1.小程序前端调用 wx.pluginLogin() 方法获取 code(loginCode)
          2.前端传入code字段给后端login接口
          3.后端调用 wxMaService.getUserService().getSessionInfo(code) 方法获取 session_key 、 openid 、 unionid
          4.后端根据 openid、unionid 查询用户信息，如果用户不存在则创建新用户
          5.后端返回 token和用户信息 给小程序前端，前端保存token，后续每次调用接口都要带上
          	此方案不能获取到用户手机号，需要用户自己绑定
         */
        ResponseResult result = weixinService.login(vo);
        return result;
    }


    @ApiOperation(value = "微信小程序openID")
    @RequestMapping(value = "/openId/{code}", method = RequestMethod.POST)
    public ResponseResult getOpenId(@NonNull  @PathVariable String code) throws IOException {
        return weixinService.getWxOpenId(code);
    }
}
